package com.tobdev.qywxinner.controller;

import com.tobdev.qywxinner.model.dto.WxUserDetailResponseDto;
import com.tobdev.qywxinner.model.entity.QywxInnerUser;
import com.tobdev.qywxinner.service.QywxInnerService;
import com.tobdev.qywxinner.service.QywxInnerUserService;
import com.tobdev.qywxinner.utils.JWTUtils;
import com.tobdev.qywxinner.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * H5应用专用
 */
@RestController
public class H5Controller {

    @Autowired
    private QywxInnerService qywxInnerService;
    @Autowired
    private QywxInnerUserService qywxInnerUserService;

    @Autowired
    private JWTUtils jwtUtils;

    @RequestMapping({"/h5/oauthUrl"})
    JsonData oauthUrl(HttpServletRequest request, @RequestParam("corp_id") String corpId, @RequestParam("oauth_callback") String oauthCallback) throws UnsupportedEncodingException {
        Map resData = new HashMap();

        //普通应用
        String oauthRedirectUrl = URLEncoder.encode(oauthCallback, "utf-8");
        String oauthUrl = qywxInnerService.getOauthUrl(corpId, oauthRedirectUrl);
        resData.put("oauth_url", oauthUrl);
        return JsonData.buildSuccess(resData);
    }


    @RequestMapping("/h5/oauthUser")
    JsonData oauthCallback(HttpServletRequest request, HttpServletResponse response, @RequestParam("corp_id") String corpId, @RequestParam("code") String code) throws IOException {
        Map<String, Object> result = new HashMap<String, Object>();
        //通过code获取信息
        WxUserDetailResponseDto wxUserDetailResponseDto = qywxInnerService.getOauthUser(corpId, code);
        //查数据库获取人员
        //人员已侦破产生token登录  //本案例仅从企业微信接口获取未从数据表中获取
        QywxInnerUser user = qywxInnerUserService.getOrInsertByCorpIdAndUserId(corpId, wxUserDetailResponseDto);
        result.put("user", user);
        String token = jwtUtils.geneJsonWebToken(user);
        result.put("token", token);
        return JsonData.buildSuccess(result);
    }


    @RequestMapping("/h5/jsAgentSign")
    JsonData getJsAgentSign(HttpServletRequest request, @RequestParam("corp_id") String corpId, @RequestParam("url") String url) throws Exception {

        String timestamp = "" + System.currentTimeMillis();
        //随机字符串
        String nonce = "56565";

        Map resData = qywxInnerService.getJsSignAgent(corpId, nonce, timestamp, url);

        return JsonData.buildSuccess(resData);

    }

    @RequestMapping("/h5/jsSign")
    public JsonData getJsSign(HttpServletRequest request, @RequestParam("corp_id") String corpId, @RequestParam("url") String url) throws Exception {
        //获取当前时间戳
        String timestamp = "" + System.currentTimeMillis();
        //随机字符串
        //todo
        String nonce = "sdfsdf";

        Map resData = qywxInnerService.getJsSign(corpId, nonce, timestamp, url);

        return JsonData.buildSuccess(resData);
    }


}
